package blbetter.visual;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import blbetter.db.BLDataBase;
import blbetter.db.Spiel;

public class BLSpieltagResultsPanel extends JPanel {

	private static final long serialVersionUID = -5148846770409019358L;

	public String[] columnNames = new String[] { "Heim", "Gast", " ", " " };

	public JTable resultTable = new JTable();

	public BLDataBase db;

	public BLSpieltagResultsPanel(BLDataBase db) {
		this.db = db;
		// setData(2, 2);
		this.setLayout(new GridBagLayout());
		this.add(new JLabel("Ergebnisse"), new GridBagConstraints(0, 0, 1, 1,
				0, 0, GridBagConstraints.NORTH, GridBagConstraints.BOTH,
				new Insets(0, 0, 0, 0), 0, 0));
		this.add(resultTable.getTableHeader(), new GridBagConstraints(0, 1, 1,
				1, 0, 0, GridBagConstraints.NORTH, GridBagConstraints.NONE,
				new Insets(0, 0, 0, 0), 0, 0));
		this.add(resultTable, new GridBagConstraints(0, 2, 1, 1, 0, 0,
				GridBagConstraints.NORTH, GridBagConstraints.NONE, new Insets(
						0, 0, 0, 0), 0, 0));
		this.setVisible(true);
		// this.setPreferredSize(new Dimension(600, 300));
	}

	public void setData(int saisonId, int spieltag) {
		// System.out.println("BLSpieltagResultsPanel.setData() : " + saisonId
		// + " - " + spieltag);
		Vector<Spiel> spiele = db.getSpieleBySaisonIdAndSpieltag(saisonId,
				spieltag);
		// System.out.println(spiele);
		String[][] tableData = new String[spiele.size()][4];
		for (int i = 0; i < spiele.size(); i++) {
			tableData[i][0] = db.getTeamName(spiele.get(i).heimId);
			tableData[i][1] = db.getTeamName(spiele.get(i).gastId);
			tableData[i][2] = "" + spiele.get(i).heimTore;
			tableData[i][3] = "" + spiele.get(i).gastTore;
		}

		resultTable.setModel(new DefaultTableModel(tableData, columnNames));

		resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		resultTable.getColumn("Heim").setPreferredWidth(200);
		resultTable.getColumn("Gast").setPreferredWidth(200);
		resultTable.getColumnModel().getColumn(2).setPreferredWidth(30);
		resultTable.getColumnModel().getColumn(3).setPreferredWidth(30);

		resultTable.setBorder(BorderFactory.createLineBorder(Color.black));
		resultTable.getTableHeader().setBorder(
				BorderFactory.createLineBorder(Color.black));
		this.repaint();
	}
}
